﻿//-----------------------------------------------------------------------------------
// WebTestRequest Plugin Template version 1.0
//-----------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebServicesTesting.Automation.WebTest.PlugIns
{
    [System.ComponentModel.DisplayName("ExceptionPlugin")]
    [System.ComponentModel.Description("TODO: Add a more detailed description of ExceptionPlugin extraction rule.")]
    public class ExceptionPlugin : WebTestRequestPlugin
    {
        #region Constructors
        /// <summary>
        /// Default Constructor, remove if not needed
        /// </summary>
        public ExceptionPlugin()
        {
        }

        #endregion // Constructors

        #region Properties
        /*
 * Public properties on the class will be surfaced to the user in the VS UI.
 * Only string, int, and bool types are allowed.
 */
        /*
                /// <summary>
                /// Sample string property
                /// </summary>
                private string m_myStringProperty;
                [System.ComponentModel.DisplayName("My String Property")]
                [System.ComponentModel.Description("Enter a long description for this property.")]
                [System.ComponentModel.DefaultValue("MyString")]
                public string MyStringProperty
                {
                    get { return m_myStringProperty; }
                    set { m_myStringProperty = value; }
                }
        */
        #endregion //Properties
        string contextParam;

        public string ContextParam
        {
            get { return contextParam; }
            set { contextParam = value; }
        }

       

        #region Overrides
        /*
 * To see a list of overridable methods type "public override " at the class scope.
 */

        /// <summary>
        /// Fires before each request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            // Access the request via e.Request property to modify the request before it is fired.

            // You can optionally control the execution of this request via 
            // the e.instruction property
            //
            //  e.Instruction = WebTestExecutionInstruction.Execute;
            //  e.Instruction = WebTestExecutionInstruction.Skip;
        }

        /// <summary>
        /// Fires after each request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
            
            try
            {
                if (((string)e.WebTest.Context[contextParam]).Equals("True"))
                {

                    
                    e.Request.Outcome= Outcome.Pass;
                 
                    e.WebTest.Context["Exception Validation"] = "True";
                }
                else
                {
                    e.Request.Outcome = Outcome.Fail;
                    e.WebTest.Context["Exception Validation"] = "False";

                }
                // You can override the outcome of the request in the post request.
            }

            catch (Exception ex)
            {
                
                
            }
            
        }
        #endregion // Overrides
    }
}